package crm.view;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Properties;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;

import crm.model.CRMConst;
import crm.model.teacherPC.CRMListener;
import crm.model.teacherPC.Manager;
import crm.model.teacherPC.net.StudentPC;
import crm.util.Resources;
import crm.util.treetable.JTreeTable;
import crm.view.desktop.StudentDesktopPane;
import crm.view.funktionBar.FunktionBar;
import crm.view.menuBar.CRMMenuBar;
import crm.view.setting.SettingPane;
import crm.view.statistic.StatisticTree;
import crm.view.statistic.treetable.StatisticTreeTableModel;
import crm.view.tray.ClassRoomManagerTray;
import crm.view.tree.students.ConnectStudentTreeView;
import crm.view.tree.verwaltung.EveryStundetsTreeControll;
import crm.view.tree.verwaltung.StudentTreeModel;
import crm.view.tree.verwaltung.StudentTreeRender;
import crm.view.tree.verwaltung.StudentTreeView;







public class ClassRoomManagerGUI extends JFrame implements WindowListener, CRMListener {


	private static final long serialVersionUID = 1L;
	private FunktionBar fBar;
	private JPanel jPanel1;
	private JScrollPane jScrollPane1;
	private JButton jButton1;
	private JTextField jTextField1;

	private JTabbedPane jTabbedPane2;
	private JSplitPane jSplitPane1;
	public StudentDesktopPane jDesktopPane1;
	private StudentTreeView jTree1;
	private JTabbedPane jTabbedPane1;
	public Manager manager;

	private JButton helpBut;

	private Box verwaltungsToolBar;
	private JButton pcVisibleBut;
	private JButton jButton4;
	private JButton addBut;
	private JPanel jPanel4;
	private JPanel jPanel3;
	private ConnectStudentTreeView everyStudents;
	private JMenuBar jMenuBar1;
	private StatisticTree statisticTree;
	private ClassRoomManagerTray tray;
	public JTreeTable treetable;
	private SettingPane settingPane;
	/**
	* Auto-generated main method to display this JFrame
	*/
	static {
		JFrame.setDefaultLookAndFeelDecorated(true);
		setLookAndFeel();

	}
	private static void setLookAndFeel(){
		//Set Look & Feel

		try {
			LookAndFeel lookandfeel;
//			 System.setProperty(
//			            "Quaqua.tabLayoutPolicy","wrap"
//			         );
//			 System.setProperty(
//			            "Quaqua.design","jaguar"
//			         );
//			 System.setProperty("Window.documentModified", "true");

//			lookandfeel = ch.randelshofer.quaqua.QuaquaManager.getLookAndFeel();
			lookandfeel = new com.jtattoo.plaf.mcwin.McWinLookAndFeel();
//			"com.jgoodies.looks.plastic.Plastic3DLookAndFeel"
//			lookandfeel = new de.javasoft.plaf.synthetica.SyntheticaSkyMetallicLookAndFeel();
//			lookandfeel = new de.javasoft.plaf.synthetica.SyntheticaStandardLookAndFeel();

			 Properties props = new Properties();
			 props.put("logoString", "JCRM");
			 props.put("licenseKey", "LICENCE");
			 com.jtattoo.plaf.mcwin.McWinLookAndFeel.setCurrentTheme(props);
			UIManager.setLookAndFeel(lookandfeel);
		} catch(Exception e) {
			e.printStackTrace();
		}

	}



	public ClassRoomManagerGUI(Manager manager) {
		super();
		this.manager = manager;
		initGUI();
		addWindowListener(this);
		manager.addListener(this);
		setVisible(true);
		setLocationRelativeTo(null);
	}

	private void initGUI() {
		setTitle("JCRM");
		setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		{//MenuBar
			jMenuBar1 = new CRMMenuBar(this);
			setJMenuBar(jMenuBar1);

		}
		{//SystemTray
			tray = new ClassRoomManagerTray(this);
		}
		try {

			
				jPanel1 = new JPanel();
				BorderLayout jPanel1Layout = new BorderLayout();
				getContentPane().add(jPanel1, BorderLayout.CENTER);
				jPanel1.setLayout(jPanel1Layout);
				{
					jSplitPane1 = new JSplitPane();
					jPanel1.add(jSplitPane1, BorderLayout.CENTER);
					jSplitPane1.setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize());
					{
						jTabbedPane1 = new JTabbedPane();
						jSplitPane1.add(jTabbedPane1, JSplitPane.LEFT);
						{
							jScrollPane1 = new JScrollPane();
							jTabbedPane1.addTab("Verwaltung", null, jScrollPane1, null);
							jScrollPane1.setPreferredSize(new java.awt.Dimension(139, 372));
							{
								jTree1 = new StudentTreeView(new StudentTreeModel(manager), manager);
								jScrollPane1.setViewportView(jTree1);
								jTree1.setCellRenderer(new StudentTreeRender());
							}
						}
						{
							jPanel3 = new JPanel();
							BorderLayout jPanel3Layout = new BorderLayout();
							jPanel3.setLayout(jPanel3Layout);
//							jTabbedPane1.addTab("Alle PCs", null, jPanel3, null);
							jPanel3.setPreferredSize(new java.awt.Dimension(139, 324));
							{
								everyStudents = new ConnectStudentTreeView(manager);
								jPanel3.add(new JScrollPane(everyStudents), BorderLayout.CENTER);

							}
							{
								jPanel4 = new JPanel();
								jPanel3.add(jPanel4, BorderLayout.SOUTH);
								FlowLayout jPanel4Layout = new FlowLayout();
								jPanel4.setLayout(jPanel4Layout);
								EveryStundetsTreeControll con = new EveryStundetsTreeControll(everyStudents, manager);
								{
									addBut = new JButton();
									addBut.addActionListener(con);
									jPanel4.add(addBut);
									addBut.setText("add");
								}
								{
									jButton4 = new JButton();
									jButton4.addActionListener(con);

									jPanel4.add(jButton4);
									jButton4.setText("remove");
								}
							}
						}
					}
					{
						jTabbedPane2 = new JTabbedPane();
						jSplitPane1.add(jTabbedPane2, JSplitPane.RIGHT);
						{
							jDesktopPane1 = new StudentDesktopPane(manager);
							jTabbedPane2.addTab("PCs", jDesktopPane1);
						}
						{
						{
							statisticTree = new StatisticTree(manager);
							jTabbedPane2.addTab("History", null, new JScrollPane(statisticTree), null);
						}
						{
							treetable = new JTreeTable(new StatisticTreeTableModel(manager));
							jTabbedPane2.addTab("History-treetable", null, new JScrollPane(treetable), null);
						}
						{
							settingPane = new SettingPane(this, manager);
							jTabbedPane2.addTab("Einstellungen", null, settingPane, null);
						}
					}
				}
				{	// Funktionsbar
					fBar = new FunktionBar(manager);
					jPanel1.add(fBar, BorderLayout.NORTH);
					fBar.setPreferredSize(new java.awt.Dimension(884, 16));

				}
				{
					verwaltungsToolBar = Box.createVerticalBox();
					jPanel1.add(verwaltungsToolBar, BorderLayout.WEST);

					{
						pcVisibleBut = new JButton(Resources.getNetWorkIcon());
						pcVisibleBut.setBorderPainted(false);
						pcVisibleBut.addActionListener(new ActionListener() {

							@Override
							public void actionPerformed(ActionEvent e) {

								jTabbedPane1.setVisible(!jTabbedPane1.isVisible());
								jTabbedPane1.repaint();
								jTabbedPane1.doLayout();
								jSplitPane1.setDividerLocation(150);
							}
						});
						verwaltungsToolBar.add(pcVisibleBut);

					}
					{
						helpBut = new JButton(Resources.getHelpIcon());
						helpBut.setBorderPainted(false);
						verwaltungsToolBar.add(helpBut);
					}
				}
			}

			this.setSize(900, 600);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		CRMConst.connected = false;
	}

	@Override
	public void windowClosing(WindowEvent arg0) {


	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}


	public void stateChanged(ChangeEvent ev) {
		JSpinner spin = (JSpinner) ev.getSource();

	}





	@Override
	public void actualisingComplete(StudentPC student) {

	}
	@Override
	public void connectionClosed() {


	}
	@Override
	public void connectionStarted() {

	}

	@Override
	public void exitCRM() {
		dispose();
	}



	@Override
	public void studentAdded(StudentPC student) {
		// TODO Auto-generated method stub

	}



	@Override
	public void studentRemoved(StudentPC student) {
		// TODO Auto-generated method stub

	}
}
